﻿using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;

namespace CodeCounsel.SharePoint2010.DigiD
{
    [Guid("61205A95-1137-4A45-8DDF-AA950E05950C")]
    public class DigiDSettings : SPPersistedObject
    {
        [Persisted]
        Uri _serverUrl;
        [Persisted]
        string _serverID;
        [Persisted]
        string _applicationID;
        [Persisted]
        string _sharedSecret;
        [Persisted]
        SecurityLevel _securityLevel = SecurityLevel.Basic;
        [Persisted]
        Uri _siteUrl;

        #region Properties
        public string AssociatedRealm
        {
            get { return Name; }
        }

        public Uri ServerUrl
        {
            get { return _serverUrl; }
            set { _serverUrl = value; }
        }

        public string ServerID
        {
            get { return _serverID; }
            set { _serverID = value; }
        }

        public string ApplicationID
        {
            get { return _applicationID; }
            set { _applicationID = value; }
        }

        public string SharedSecret
        {
            get { return _sharedSecret; }
            set { _sharedSecret = value; }
        }

        public SecurityLevel SecurityLevel
        {
            get { return _securityLevel; }
            set { _securityLevel = value; }
        }

        public Uri SiteUrl
        {
            get { return _siteUrl; }
            set { _siteUrl = value; }
        }

        #endregion

        #region Constructors

        public DigiDSettings()
        {
        }

        internal DigiDSettings(string name, DigiDService parent)
            : base(name, parent)
        {
        }

        #endregion

        protected override void Validate()
        {
            if (_securityLevel != SecurityLevel.Basic &&
                _securityLevel != SecurityLevel.Medium)
            {
                throw new ArgumentOutOfRangeException("SecurityLevel");
            }
            if(_siteUrl != null && 
                (_siteUrl.Scheme != Uri.UriSchemeHttp &&
                _siteUrl.Scheme != Uri.UriSchemeHttps))
            {
                throw new ArgumentOutOfRangeException("SiteUrl should be HTTP or HTTPS");
            }
            if (_serverUrl != null && _serverUrl.Scheme != Uri.UriSchemeHttps)
            {
                throw new ArgumentOutOfRangeException("ServerUrl should be HTTPS");
            }
            base.Validate();
        }
    }
}
